package com.cmccgz.udacs_landlord_webapp.main.accreditmanager;

import android.support.annotation.NonNull;

import com.cmccgz.udacs_landlord_webapp.base.ApiService;
import com.cmccgz.udacs_landlord_webapp.bean.CardInfo;
import com.cmccgz.udacs_landlord_webapp.bean.ResultBean;
import com.cmccgz.udacs_landlord_webapp.utils.ServerDateUtils;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by June on 2017-10-31.
 */

public class AddCardPresenter implements AddCardContract.Presenter {

    private AddCardContract.View contentView;

    public AddCardPresenter(@NonNull AddCardContract.View contentView){
        this.contentView = contentView;
        this.contentView.setPresenter(this);
    }

    @Override
    public void start() {

    }

    @Override
    public void getServerTime() {
        getServerTimeImp(new Subscriber<Long>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                contentView.setServerTime(-1);
            }

            @Override
            public void onNext(Long aLong) {
                contentView.setServerTime(aLong);
            }
        });

    }

    private void getServerTimeImp(Subscriber<Long> subscriber) {
        Observable.just("")
                .subscribeOn(Schedulers.newThread())
                .unsubscribeOn(Schedulers.newThread())
                .map(new Func1<Object, Long>() {
                    @Override
                    public Long call(Object o) {
                        long serverDateMis=-1;
                        try {
                             serverDateMis = ServerDateUtils.getServerDateMis();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        return serverDateMis;
                    }
                }).observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    @Override
    public void getCardInfo(final String mobile, final String houseId, final long serverTime) {
        if (serverTime>0) {
            getCardInfoImp(mobile,houseId,serverTime);
        }else{
            getServerTimeImp(new Subscriber<Long>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {
                    contentView.showCardError("获取服务器时间失败，请稍后再试!");
                }

                @Override
                public void onNext(Long aLong) {
                    getCardInfoImp(mobile,houseId,serverTime);
                }
            });
        }
    }

    private void getCardInfoImp(String mobile, String houseId, long serverTime) {
        ApiService.getCardNo(mobile,houseId,serverTime+"").subscribe(new Subscriber<ResultBean<CardInfo>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                contentView.showCardError("获取卡号失败，请稍后再试");
            }

            @Override
            public void onNext(ResultBean<CardInfo> cardInfoResultBean) {
                if ("0".equals(cardInfoResultBean.rsmsg.rscode)) {
                    contentView.showCardNo(cardInfoResultBean.resp);
                }else{
                    contentView.showCardError(cardInfoResultBean.rsmsg.rsdesc);
                }
            }
        });
    }

   /* @Override
    public void addCard(String mobile, String houseId, String cardType, String uuid) {
        ApiService.addCard(mobile,houseId,uuid,cardType).subscribe(new Subscriber<ResultBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                contentView.addCardFail("添加卡片失败！");
            }

            @Override
            public void onNext(ResultBean resultBean) {
                if ("0".equals(resultBean.rsmsg.rscode)) {
                    contentView.addCardSuccess();
                }else{
                    contentView.addCardFail(resultBean.rsmsg.rsdesc);
                }
            }
        });
    }*/
}
